﻿
package com.ostrich.manpower.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.base.util.StringUtil;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrMealaddinfo;
import com.ostrich.manpower.common.ImportUtil;
import com.ostrich.manpower.service.TostHrMealaddinfoDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostHrMealaddinfoService")
public class TostHrMealaddinfoService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostHrMealaddinfoService.class);

	@Autowired(required=false)
    private TostHrMealaddinfoDao<T> dao;

	public TostHrMealaddinfoDao<T> getDao() {
		return dao;
	}
	@Autowired
	private TostHrBaseinfoService tosthrbaseinfoservice;
	@Autowired(required=false)
	private TostHrMealaddinfoService<TostHrMealaddinfo> tosthrmealaddinfoservice;
	
	public List<T> queryByhrDepart(String departmenma){
		return getDao().queryByhmeDepartment(departmenma);
	}
	
	public List<T> queryByAll() throws Exception{
		return getDao().queryByAll();
	}
	
	public void deleteByhrMonth(String hrMonth,int hrId){
		dao.deleteByhrMonth(hrMonth,hrId);
	}
	
	//自动生成：通过月份，id查询
	public List<T> queryIdHridHme(String hmeMonth,int hrId){
		return dao.queryIdHridHme(hmeMonth,hrId);
	}
	
	
	/****
	 * 判断月份是否存在
	 * @param hmeMonth
	 * @return
	 */
	private boolean isExistByHmemonth(String hmeMonth) {
		int coun= getDao().isExistByHmemonth(hmeMonth);
		return coun>0?true:false;
	}

	/****
	 * 根据sheet插入数据库
	 * @param sheet
	 * @return
	 */
	public String setMealBySheet(HSSFSheet sheet,String month) throws Exception{
		String msg = "";
		HSSFRow row = null;
	    int colnum = 8;
	    //判断该月是否存在，存在给予提示切不导入
	    if(isExistByHmemonth(month)){
	    	msg="数据库已经导入过该月数据，如果需要重新导入请删除该月历史数据。";
	    	return msg;
	    }
		 //遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数  除去标题跟表头
	     for(int j = 2; j < sheet.getPhysicalNumberOfRows(); j++) {  //j代表行数
	         row = sheet.getRow(j);  
	         TostHrMealaddinfo tmeal = new TostHrMealaddinfo();  
	         String iscontinut = (row!=null)?ImportUtil.getCellValue(row.getCell(1)):null;
        	 TostHrBaseinfo tbf= (TostHrBaseinfo) tosthrbaseinfoservice.getBaseinfoByHrCode(iscontinut);
        	 tbf = (tbf==null)?new TostHrBaseinfo():tbf;
        	 msg +=(tbf!=null&&tbf.getId()!=null)?"":"第"+j+"行记录异常：工号："+iscontinut+"，姓名为："+ImportUtil.getCellValue(row.getCell(2))+"的餐补没有正常导入。<br/>";
	         if(iscontinut!=null && iscontinut.length()>0){
		         for(int i=0;i<colnum;i++ ){//i代表列数
		        	 String str = ImportUtil.getCellValue(row.getCell(i));
		        	 if(i==1){
			        	 if(tbf!=null && tbf.getId()!=null)
			        	 	tmeal.setHrId(tbf.getId());
		        	 }else if(i==2){
		        		 tmeal.setHmeName(tbf.getHrName());
		         	 }else if(i==3){//部門
		        		 tmeal.setHmeDepartment(tbf.getHrDepart());
		        	 }else if(i==4){//补贴
		        		 Float flstr = (str!=null && str.trim().length()>0 &&str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
		        		 tmeal.setHmeMealsubsidies(flstr);
		        	 }else if(i==5){//扣除
		        		 Float flstr = (str!=null  && str.trim().length()>0 && str.matches("^[0-9]+\\.{0,1}[0-9]{0,2}$"))?Float.parseFloat(str):new Float(0);
		        		 tmeal.setHmeMealeduction(flstr);
		        	 }else if(i==6){//备注
		        		 tmeal.setHmeDesc(str);
		        	 }
		         }
	         }else{
	        	 break;
	         }
	         if(tmeal!=null && tmeal.getHmeName()!=null && tmeal.getHmeName().trim().length()>0){
	        	// Map<String Object> map=FormulaUtil.formulaById(1, obj);
	        	 tmeal.setHmeMonth(month);
		         dao.add((T) tmeal);
		         //infos.add(tmeal);
	         }
	     }
	     return msg;
	}

	public List<T> queryMealBytosthrmealadd(TostHrMealaddinfo tosthrmealaddinfo,String startM,String endM){
		return getDao().queryMealBytosthrmealadd(tosthrmealaddinfo, startM, endM);
	}
	
	
}